package cn.sdstudio.utils;


import cn.sdstudio.config.CustomUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Optional;

/**
 * @author Zyq
 * @version 1.0
 * @date 2025/5/17 周六
 */
public class SecurityUtils {

    private SecurityUtils() {}


    /**
     * 获取当前登录用户
     * @return 当前用户信息
     */
    public static Optional<CustomUserDetails> getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated() &&
            authentication.getPrincipal() instanceof CustomUserDetails) {
            return Optional.of((CustomUserDetails) authentication.getPrincipal());
        }
        return Optional.empty();
    }

}
